package com.sc.nft.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.nft.annotation.UserLogin;
import com.sc.nft.controller.request.*;
import com.sc.nft.controller.vo.RechargeAmountVO;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.*;
import com.sc.nft.enums.SendSMSEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.CheckParameterService;
import com.sc.nft.service.DepositWithdrawalRecordService;
import com.sc.nft.service.ExpenditureRecordService;
import com.sc.nft.service.UserWalletService;
import com.sc.nft.sup.ErrorCode;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/wallet")
@Api(tags ="钱包")
public class UserWalletController {
    private final UserWalletService userWalletService;
    private final ExpenditureRecordService expenditureRecordService;
    private final DepositWithdrawalRecordService depositWithdrawalRecordService;
    private final CheckParameterService checkParameterService;


    @ApiOperation(value = "查看用户钱包列表", notes = "查看用户钱包列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWalletList", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<ArrayList<UserWalletVO>> getWalletList() {
        return Result.success(userWalletService.getWalletList(UserPool.getUser()));
    }
    @ApiOperation(value = "查看用户钱包", notes = "查看用户钱包", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWallet", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<UserWalletVO> getWallet(@Valid @RequestBody GetWalletRequest request) {
        UserWalletVO byUser = userWalletService.getByUser(UserPool.getUser(), request.getType());
        return Result.success(byUser);
    }

    @ApiOperation(value = "查看账单明细", notes = "查看账单明细", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getExpenditureRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<ExpenditureRecordVO> getExpenditureRecord(@Valid @RequestBody ExpenditureRecordRequest request) {
        Page<ExpenditureRecordVO> page=expenditureRecordService.getExpenditureRecord(UserPool.getUser().getId(),request.getCalendar(),request.getPageNo(),request.getPageSize(),request.getType());
        return PageResult.success(page);
    }

    @ApiOperation(value = "提现", notes = "提现", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/withdraw", produces = MediaType.APPLICATION_JSON_VALUE)
    public Result withdraw(@Valid @RequestBody WithdrawRequest request) {

        if(true){
            throw new GlobalRunTimeException(ErrorCode.THE_SYSTEM_IS_BEING_UPGRADED_PLEASE_UPDATE_TO_THE_LATEST_VERSION_LATER);
        }


        return Result.success();
    }
    @ApiOperation(value = "充值记录", notes = "充值记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/rechargeRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<WithdrawOrRechargeRecordVO>  rechargeRecord(@Valid @RequestBody WithdrawalsAndRechargeRequest request) {
        Page<WithdrawOrRechargeRecordVO>page=depositWithdrawalRecordService.rechargeRecord(UserPool.getUser().getId(),request.getCalendar(),request.getPageNo(),request.getPageSize());
        return PageResult.success(page);
    }

    @ApiOperation(value = "提现记录", notes = "提现记录", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/withdrawalsRecord", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public PageResult<WithdrawOrRechargeRecordVO> withdrawalsRecord(@Valid @RequestBody WithdrawalsAndRechargeRequest request) {
        Page<WithdrawOrRechargeRecordVO>page=depositWithdrawalRecordService.withdrawalsRecord(UserPool.getUser().getId(),request.getCalendar(),request.getPageNo(),request.getPageSize());
        return PageResult.success(page);
    }

    @ApiOperation(value = "获取充值金额", notes = "获取充值金额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getRechargeAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<List<RechargeAmountVO>> getRechargeAmount() {
        return Result.successColl(depositWithdrawalRecordService.getRechargeAmount(),RechargeAmountVO.class);
    }

    @ApiOperation(value = "获取提现详情", notes = "获取提现详情", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWithdrawDetails", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetWithdrawDetailsVO> getWithdrawDetails(@Valid @RequestBody WalletRequest request) {
        return Result.success(userWalletService.getWithdrawDetails(UserPool.getUser(),request.getChannel()));
    }

    @ApiOperation(value = "获取提现到账金额", notes = "获取提现到账金额", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping(value = "/getWithdrawAmount", produces = MediaType.APPLICATION_JSON_VALUE)
    @UserLogin
    public Result<GetWithdrawFeeVO> getWithdrawAmount(@Valid @RequestBody GetWithdrawFeeRequest request) {
        return Result.success(userWalletService.getWithdrawAmount(request.getAmount(),request.getType(),request.getChannel()));
    }
}